package br.com.midiaseducativas.tutor.lo.imagetextsound {
	import br.com.midiaseducativas.tutor.events.FeedBackEvent;
	import br.com.midiaseducativas.tutor.events.SpeechEvent;
	import br.com.midiaseducativas.tutor.lo.imagetextsound.vo.ImageTextSoundVO;
	
	import com.demonsters.debugger.MonsterDebugger;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	
	public class LOCore {
		
		private var loStage:LOStage;

		private var xmlLoader:URLLoader;
		
		private var loArray:Array;
		
		private var loCounter:int;
		
		private var imgLoader:Loader;
		
		
		public function LOCore(pLOStage:LOStage, pXMLString:String) {
			
			try {
				this.xmlLoader = new URLLoader();
				this.xmlLoader.addEventListener(Event.COMPLETE, onLoaderComplete);
				//MonsterDebugger.trace(this, "Passou 1 - "+ pXMLString);
				this.xmlLoader.load(new URLRequest(pXMLString));
				//MonsterDebugger.trace(this, "Passou 2");
				this.loStage = pLOStage;
				this.loCounter = 0;
			} catch (e:Error){
				
			}
			
			
		}

		protected function onLoaderComplete(event:Event):void
		{
			//MonsterDebugger.trace(this, this.xmlLoader.data);
			var xmlLO:XML = new XML(this.xmlLoader.data);
			
			var lo:ImageTextSoundVO;
			//MonsterDebugger.trace(this, xmlLO);
			
			this.loArray = new Array();
			
			
			for (var name:String in xmlLO.item) {
				
				lo = new ImageTextSoundVO();
				lo.img = xmlLO.item[name].img;
				lo.text = xmlLO.item[name].text;
				lo.sound = xmlLO.item[name].sound;
				
				this.loArray.push(lo);
			
			}
			this.startLO();
		}
		
		private function startLO():void {
			
			MonsterDebugger.trace(this, this.loArray[this.loCounter]);
			//this.loStage
			this.showSlide(this.loArray[this.loCounter]);
			this.loStage.sound.addEventListener(MouseEvent.CLICK, onSoundClick);
			this.loStage.next.addEventListener(MouseEvent.CLICK, onNextClick);
			this.loStage.prev.addEventListener(MouseEvent.CLICK, onPrevClick);
			
		}
		
		private function showSlide(lo:ImageTextSoundVO):void {
			
			this.loStage.slideCounter.text = "Slide "+(this.loCounter+1)+" de "+this.loArray.length;
			
			try {
				if (this.loStage.img.contains(this.imgLoader)){
					this.loStage.img.removeChild(this.imgLoader);
					this.imgLoader = null;
				}
			} catch (e:Error) {
				MonsterDebugger.trace(this, e);
			}
			this.imgLoader = new Loader();
			this.imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadedImg);
			this.imgLoader.load(new URLRequest(lo.img));
			
			this.loStage.text.htmlText = lo.text;
			
		}
		
		protected function onLoadedImg(event:Event):void {
			
			this.loStage.img.addChild(this.imgLoader);
			
			
			if (this.imgLoader.width < this.imgLoader.height) {
				
				this.imgLoader.height = 350;
				this.imgLoader.scaleX = this.imgLoader.scaleY;
			}else {
				this.imgLoader.width = 350;
				this.imgLoader.scaleY = this.imgLoader.scaleX;
			}
			MonsterDebugger.trace(this, this.imgLoader);
			
			this.imgLoader.x -= this.imgLoader.width/2;
			this.imgLoader.y -= this.imgLoader.height/2;
			
		}
		
		protected function onSoundClick(pEvent:MouseEvent):void {
			
			var event:SpeechEvent = new SpeechEvent(SpeechEvent.SAY, this.loArray[this.loCounter].sound, true);
			this.loStage.stage.dispatchEvent(event);
			MonsterDebugger.trace(this, event);
			
		}
		
		protected function onNextClick(pEvent:MouseEvent):void {
			this.loCounter++;
			
			if (this.loCounter < this.loArray.length) { 
				this.showSlide(this.loArray[this.loCounter]); 
			}else {
				this.loCounter = this.loArray.length-1;
				// COLOCAR O EVENTO AKI
				// COLOCAR O EVENTO AKI
				// COLOCAR O EVENTO AKI
				// COLOCAR O EVENTO AKI
				// COLOCAR O EVENTO AKI
				// COLOCAR O EVENTO AKI
				MonsterDebugger.trace(this, "COLOCAR O EVENTO AKI de Finalização");
			}
		}
		
		protected function onPrevClick(event:MouseEvent):void {
			this.loCounter--;
			if (this.loCounter >= 0) { 
				this.showSlide(this.loArray[this.loCounter]); 
			}else {
				this.loCounter = 0;
			}
			
		}
		
		
		
	}
}